// pages/ticket/index.js
const app = getApp();
const req = require("../../utils/http.js");
Page({


  data: {
    type: 0,
    searchSongList: [],
    page: 1,
    size: 10,
    state: '0',
    newList: [],
    showText:'未使用'
  },
  bindType: function(e) {
    let page = 1
    let size = this.data.size
    if (e.currentTarget.dataset.index == 1) {
      this.setData({
        'showText': "已使用",
        newList:[]
      });
    } else if (e.currentTarget.dataset.index == 2) {
      this.setData({
        'showText': "已过期",
        newList: []
      });
    } else {
      this.setData({
        'showText': "未使用",
        newList: []
      });
    }
    this.setData({
      'type': e.currentTarget.dataset.index,
      'state': e.currentTarget.dataset.index,

    });

    //拉取列表信息
    this.fetchSearchList(page, size);
  },
  clikeIndex(){
    wx.switchTab({
      url: '../index/index',
    })
  },
  // 点击未使用列表跳转详情页
  CouponClike(e){
    let {type,id}=e.currentTarget.dataset
    let showText = this.data.showText
    if(showText=='未使用' && type==3){//type==3商家券
      wx.navigateTo({
        url: '../couponDetails/couponDetails?id='+id,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let t = this
    let page = 1
    let size = t.data.size
    this.fetchSearchList(page, size);
  },

  fetchSearchList(page, size) {
    let t = this;
    let state = t.data.state;
    let params = {
      state
    }
    wx.showLoading({
      title: '玩命加载中...',
    })
    req._objPost(`/coupon/wxProduct/getHaveProduct/${page}/${size}`, 'POST', params).then(res => {
      let list = res.data.result.records
      let newList = t.data.newList.concat(list);
      t.setData({
        newList,
        total: res.data.result.total
      })
      wx.hideLoading();
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let t = this
    wx.showLoading({
      title: '玩命加载...',
    })
    var page = t.data.page + 1;
    t.setData({
      page: page
    })
    if (page > Math.ceil(t.data.total / t.data.size)) {
      wx.showToast({
        title: '到底了！！！',
        icon: 'none'
      })
      return;
    }
    t.fetchSearchList(page, t.data.size);
    wx.hideLoading()
  },

  onShareAppMessage: function() {
    return {
      title: '优惠券',
      desc: '优惠多多，惊喜多多!',
      path: '/pages/index/index'
    }
  }
})
